jump to navigation

Character decoding Error while retriving parameters in Servlet. August 21, 2008

Posted by ninadgawad in Javascript.
Tags: , , ,
trackback

Problem:

WARNING: Parameters: Character decoding failed. Parameter skipped.
java.io.CharConversionException: isHexDigit

Cause:

  • Generally we get this error when we try to add special characters like “%, @, $ .. etc” in the GET/POST request from Ajax Call.
  • Since these characters are used to URL specific function like “&” id used to differentiate various URL parameters. This generates a “null” value for the parameter you will try to retrieve in Servlet.

Solution:

The simple way to get over this problem is to massage your Special Character String with “escape()” funtion in javascript.

Example:

Before:

var vUserEnteredData = document.formname.textbox.value ;  //  textbox = 100%

var parameters = “testId=A101&percentageNeeded=” + vUserEnteredData;

// Some Ajax Code …..

request.setRequestHeader( ‘Content-Type’, ‘application/x-www-form-urlencoded; charset=UTF-8’ );
request.setRequestHeader(“Content-length”, parameters.length);
request.setRequestHeader(“Connection”, “close”);
request.send(parameters);

// Note: Here the “%” will cause java.io.CharConversionException: while retriving the percentageNeeded parameter in Servlet.

After:

var vUserEnteredData = document.formname.textbox.value ;  //  textbox = 100%

var parameters = “testId=A101&percentageNeeded=” + escape(vUserEnteredData);

// Some Ajax Code …..

request.setRequestHeader( ‘Content-Type’, ‘application/x-www-form-urlencoded; charset=UTF-8’ );
request.setRequestHeader(“Content-length”, parameters.length);
request.setRequestHeader(“Connection”, “close”);
request.send(parameters);

Alternate Solution:

  • We can use Ajax Frameworks rather than manually making the Ajax Request like PrototypeJS, DWR etc.

Comments»

1. asdasd - October 13, 2008

sadasd

2. Anonymous - August 31, 2010

Thanks, this helped me!

3. subha - November 4, 2010

thanks a lot

4. Vincent - June 1, 2011

thanks this helped me

5. Anonymous - June 24, 2011

SIP, thanks u very much

6. Anonymous - July 28, 2011

It’s work in part, thank you

7. Bibhaw - November 15, 2011

Thank you.

8. Anonymous - December 10, 2011

Great Job.

9. Anonymous - January 11, 2012

This is priceless, thank you very much for posting!

10. rishabhshah0330 - May 16, 2012

I am using escape(note) before sending the “note” as parameter and URLDecoder.decode(note,”UTF-8″) for decoding the content on java side. However, when there are special characters, I do get following warning,

WARNING: Parameters: Character decoding failed. Parameter skipped.
java.io.CharConversionException: isHexDigit

I am concerned whether it can cause problem in future. Is it safe to do this?

11. Anonymous - November 15, 2012

Thanks It helped me But + symbol didnot working…

12. Anonymous - December 20, 2012

Its work fine. thanks

13. Anonymous - December 20, 2012

Hi, this is really worthful. but only one small thing + symbol didn’t work . Kindly suggest any idea to fix this. Thanks in Advance.

14. Anonymous - January 8, 2013

hi folks, really thanks alot…

15. Anonymous - May 6, 2013

Thanks a lot 🙂

16. Anonymous - May 6, 2013

Very clearly explained…..
Thanks a lot………

17. Shoubhik Bose (@sbose78) - June 30, 2013

Thanks ! 🙂

18. saurabhjblogger - July 12, 2013

Yes It works . Great Post
There is an alternate solution to this.

we you mention encoding format in the connector and configure a CharacterEncodingFilter. It will solve the problem also.

19. saurabhjblogger - July 12, 2013

sorry typo.
you can mention encoding format in the connector in (server.xml) and configure a CharacterEncodingFilter. It will solve the problem .

20. saurabhjblogger - July 12, 2013

put this code in your web.xml and it will take care of your entire appliaction , you have no need to go and change each and every .js or .jsp file to use function.

characterEncodingFilter
org.apache.catalina.filters.SetCharacterEncodingFilter

encoding
UTF-8

force
true

characterEncodingFilter
/*

21. linsey - May 20, 2016

thank you this was really helpful and allowed me to find my bug!


Leave a comment